﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Application.Common.Utils;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.UserTables.Queries
{
    public class LoginQueries : IRequest<Json<LoginDTO>>
    {
        public string account { get; set; }
        public string password { get; set; }
        /// <summary>
        /// 登录类型：0  客户端 1 平台端
        /// </summary>
        public int loginType { get; set; } = 0;
        public string source { get; set; }
    }

    public class LoginQueriesHandler : IRequestHandler<LoginQueries, Json<LoginDTO>>
    {
        private readonly IUsertableRepository _iuserTableRepository;
        private readonly ITokenUserService _itokenUserServices;
        private readonly IRoletableRepository _iroleTableRepository;
        private readonly IMenuTableRepository _imenutableRepository;
        public LoginQueriesHandler(IUsertableRepository iuserTableRepository, ITokenUserService itokenUserServices, IRoletableRepository iroleTableRepository, IMenuTableRepository imenutableRepository)
        {
            _iuserTableRepository = iuserTableRepository;
            _itokenUserServices = itokenUserServices;
            _iroleTableRepository = iroleTableRepository;
            _imenutableRepository = imenutableRepository;
        }
        public async Task<Json<LoginDTO>> Handle(LoginQueries request, CancellationToken cancellationToken)
        {

            if (request != null)
            {
                if (string.IsNullOrWhiteSpace(request.account))
                {
                    return Json<LoginDTO>.Failed("请输入账号");
                }
                else if (string.IsNullOrWhiteSpace(request.password))
                {
                    return Json<LoginDTO>.Failed("请输入密码");
                }
                else if (request.loginType != 0 && request.loginType != 1)
                {
                    return Json<LoginDTO>.Failed("参数不合法");
                }
                //else if (request.account.Length < 8)
                //{
                //    return Json<LoginDTO>.Failed("账号至少输入8位");
                //}
                //else if (request.account.Length > 20)
                //{
                //    return Json<LoginDTO>.Failed("账号最多输入20位");
                //}
                //else if (request.password.Length < 8)
                //{
                //    return Json<LoginDTO>.Failed("密码至少输入8位");
                //}
                //else if (request.password.Length > 20)
                //{
                //    return Json<LoginDTO>.Failed("密码最多输入20位");
                //}
                else
                {
                    string pwd = MD5Encrypt.GetMd5Hash(request.password);
                    var model = await _iuserTableRepository.Login(request.account, pwd, cancellationToken);

                    if (model != null && model.id > 0)
                    {

                        var role = await _iroleTableRepository.GetRoleTableByRoleId(model.roleid, cancellationToken);
                        if (!(role != null && role.id > 0))
                        {
                            return Json<LoginDTO>.Failed("当前账号异常，请联系管理员排查");
                        }
                        int roletype = role.roletype ?? 0;
                        if (request.loginType == 0 && roletype != 2 && roletype != 3)
                        {
                            return Json<LoginDTO>.Failed("请登录平台端");
                        }
                        else if (request.loginType == 1 && roletype != 1)
                        {
                            return Json<LoginDTO>.Failed("请登录客户端");
                        }
                        List<menutable> list = await _imenutableRepository.GetMenuTableList(role.id, 0, cancellationToken);
                        LoginDTO return_model = new LoginDTO()
                        {
                            id = model.id,
                            account = model.account,
                            name = model.name,
                            address = model.address,
                            roleid = model.roleid,
                            roleName = role.name,
                            roletype = role.roletype ?? 0,
                            roleList = list
                        };
                        return_model.token = _itokenUserServices.CreateToken(return_model);
                        return new Json<LoginDTO>() { Status = 1000, Detail = "登录成功", Data = return_model };
                    }
                    else
                    {
                        return new Json<LoginDTO>() { Status = 1003, Detail = "账号或密码错误" };
                    }
                }
            }
            else
            {
                return new Json<LoginDTO>() { Status = 1003, Detail = "参数不能为空" };
            }
        }
    }
}
